package route

import (
	"clouds.lgb24kcs.cn/controllers/bucket/add"
	"clouds.lgb24kcs.cn/controllers/bucket/bucketselect"
	"clouds.lgb24kcs.cn/controllers/bucket/del"
	"clouds.lgb24kcs.cn/controllers/bucket/detail"
	"clouds.lgb24kcs.cn/controllers/bucket/list"
	catalogue2 "clouds.lgb24kcs.cn/controllers/catalogue"
	"clouds.lgb24kcs.cn/controllers/catalogue/catalogudel"
	"clouds.lgb24kcs.cn/controllers/catalogue/create"
	"clouds.lgb24kcs.cn/controllers/catalogue/detailsobject"
	"clouds.lgb24kcs.cn/controllers/catalogue/search"
	"clouds.lgb24kcs.cn/controllers/files/createobject"
	"clouds.lgb24kcs.cn/controllers/files/deleteobject"
	"clouds.lgb24kcs.cn/controllers/files/download"
	"clouds.lgb24kcs.cn/controllers/files/listobject"
	"clouds.lgb24kcs.cn/controllers/files/searchobject"
	"clouds.lgb24kcs.cn/controllers/historys"
	"clouds.lgb24kcs.cn/controllers/share/create_share"
	"clouds.lgb24kcs.cn/controllers/share/list_share"
	"os"

	"clouds.lgb24kcs.cn/controllers/user/Login"
	"clouds.lgb24kcs.cn/controllers/user/Register"
	"clouds.lgb24kcs.cn/controllers/user/upload"
	"clouds.lgb24kcs.cn/controllers/user/userInfo"
	_ "clouds.lgb24kcs.cn/docs"
	"clouds.lgb24kcs.cn/global"
	"clouds.lgb24kcs.cn/middleware"
	"github.com/gin-gonic/gin"
	swaggerfiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
)

var DIR, _ = os.Getwd()

func StartRouter() *gin.Engine {
	engine := gin.Default()
	engine.Use(middleware.Cors())
	engine.Static("/static", DIR+global.APP.Configuration.Server.Upload)
	user := engine.Group("user/v1")

	engine.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))

	{
		user.POST("login", Login.Login)
		user.GET("gain_sm_code", Login.GainSMCode)
		user.POST("sm_code_login", Login.SmCodeLogin)
		user.POST("register", Register.Register)

		user.Use(middleware.ChecKToken())
		{
			user.GET("userinfo", userInfo.UserInfo)
			user.PUT("userupdata", upload.UserUpdata)

		}
	}
	bucket := engine.Group("bucket/v1")
	bucket.Use(middleware.ChecKToken())
	{
		bucket.POST("createbucket", add.Add)
		bucket.GET("list", list.List)
		bucket.GET("detail", detail.DetailBucket)
		bucket.GET("select", bucketselect.BucketSelect)
		bucket.DELETE("del", del.DELBucket)
	}
	catalogue := engine.Group("catalogue/v1")
	catalogue.Use(middleware.ChecKToken())
	{
		catalogue.POST("create", create.Create)
		catalogue.GET("detail", detailsobject.DetailCatalogue)
		catalogue.GET("home_catalogue", catalogue2.HomeCatalogue) // 首页目录数据加载
		catalogue.GET("cataloguesearch", search.Search)
		catalogue.DELETE("delete", catalogudel.CatalogueDelete)
	}
	file := engine.Group("file/v1")
	file.Use(middleware.ChecKToken())
	{
		file.PUT("uploadchunk", createobject.UploadChunk)
		file.GET("megerchunk", createobject.MegerChunk)
		file.GET("objectlists", listobject.Lists)
		file.GET("objectsearch", searchobject.ObjectSearche)
		file.DELETE("objectdelete", deleteobject.ObjectDelete)
		file.GET("download", download.Download)
	}
	history := engine.Group("history/v1")
	history.Use(middleware.ChecKToken())
	{
		history.GET("info", historys.HistoryAll)
	}
	shares := engine.Group("share/v1")
	shares.Use(middleware.ChecKToken())
	{
		shares.POST("create_share", create_share.CreateShare)
		shares.GET("list_share", list_share.ListShare)
	}
	return engine
}

func Start() {
	router := StartRouter()
	router.Run(":" + global.APP.Configuration.Server.Port)
}
